package com.llsydn.demo.selectContact;

import android.Manifest;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.llsydn.hello.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 联系人电话列表
 * @description 请填写相关描述
 * @date 2018/9/30 18:02
 */
public class ContactListActivity extends ListActivity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private ContactAdapter adapter;
    private List<Map<String, String>> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);

        listView = getListView();
        adapter = new ContactAdapter();
        //查询得到联系人列表数据

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 0);
            //返回
            finish();
        } else {
            ContentResolver resolver = getContentResolver();
            String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
            Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
            while (cursor.moveToNext()) {
                String name = cursor.getString(0);
                String number = cursor.getString(1);
                Map<String, String> map = new HashMap<>();
                map.put("name", name);
                map.put("number", number);
                data.add(map);
            }
            listView.setAdapter(adapter);

            //给ListView添加监听
            listView.setOnItemClickListener(this);
        }
    }

    /**
     * listview的item点击事件
     *
     * @param adapterView
     * @param view
     * @param i
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //设置结果
        String number = data.get(i).get("number");
        Intent intent = getIntent();
        intent.putExtra("NUMBER", number);
        setResult(RESULT_OK, intent);
        //返回
        finish();
    }

    class ContactAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View converView, ViewGroup viewGroup) {
            if (converView == null) {
                converView = View.inflate(ContactListActivity.this, R.layout.item_contact, null);
            }
            Map<String, String> map = data.get(i);
            TextView nameTv = converView.findViewById(R.id.tv_item_name);
            TextView numberT = converView.findViewById(R.id.tv_item_number);
            nameTv.setText(map.get("name"));
            numberT.setText(map.get("number"));
            return converView;
        }
    }

}
